// pages/index/neirong/neirong.js
Page({

    data: {
        neirong:'',
        comment:'',
        comments:''
    },
    onLoad: function(options){
        let data = JSON.parse(options.current)
        this.setData({
            neirong:data
        })
      },
      onShow(){
            var that=this
            wx.cloud.database().collection('comment').where({
            _id:that.data.neirong._id
        }).get({
            success(res){
                var a=res.data[0].data.reverse()
                that.setData({
                    comments:res.data[0].data
                })
            }
        })
      },
      post(){
        if(this.data.comment.length<10){
            wx.showModal({
                title: '内容小于10个字，请重新输入',
                showCancel: false
              })
          }else if(this.data.comment.length>150){
            wx.showModal({
                title: '内容大于150个字，请重新输入',
                showCancel: false
              })
          }
          else{
              var that=this
            wx.cloud.callFunction({
                name: 'msgCheck',
                data: {
                  content: that.data.comment, 	//传入我得到的文本内容
                }
              }).then(ckres => {
                console.log(ckres.result.errCode)
                if(ckres.result.errCode==0){
                    var that=this
                    var userinfo=wx.getStorageSync('user')
                  wx.cloud.database().collection('comment').where({
                              _id:that.data.neirong._id
                          }).get({
                              success(res){
                                  if(res.data.length==0){
                                      var a=that
                                      var dir={touxiang:userinfo.avatarUrl,nickName:userinfo.nickName,word:that.data.comment}
                                      console.log(dir);
                                      wx.cloud.database().collection('comment').add({
                                          data:{
                                              _id:a.data.neirong._id,
                                              data:[dir]
                                          }
                                      })
                                      
                                      wx.showToast({
                                        title:'评论成功',
                                        icon:'success',
                                        duration:2000
                                        })
                                        setTimeout(() => {
                                            that.onShow()
                                        }, 2000);
                                  }else{
                                      var dir={nickName:userinfo.nickName,touxiang:userinfo.avatarUrl,word:that.data.comment}
                                      var temp=that.data.comments
                                      temp.push(dir)
                                      console.log(that.data.neirong._id);
                                      wx.cloud.database().collection('comment').doc(that.data.neirong._id).update({
                                          data:{
                                              data:temp
                                          }
                                         
                                      })
                                      wx.showToast({
                                        title:'评论成功',
                                        icon:'success',
                                        duration:2000
                                        })
                                        setTimeout(() => {
                                            that.onShow()
                                        }, 2000);
                                  }
                              }
                          })
                }else{
                    wx.showModal({
                        title: '内容包含敏感词汇，请重新输入',
                        showCancel: false
                      })
                }
              })
      }}
})